package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.GoodsListActivity;
import com.future.mayi.adapter.CouponsAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.CouponsModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.CouponsResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class CouponsFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.iv_null)
    ImageView ivNull;

    /**
     * type
     * 0-未使用 1-已使用 10-已过期
     * 4领取
     */
    private int type;
    private int page = 0;

    private CouponsAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_coupons;
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new CouponsAdapter(getActivity(), recyclerView, type);
        recyclerView.setAdapter(adapter);
        recyclerView.setEmptyView(ivNull);
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        recyclerView.refresh();
    }


    public void setType(int type) {
        this.type = type;
        if (adapter != null)
            adapter.setType(type);
    }


    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    @Override
    public void OnItem(View view, int position) {
        CouponsModel couponsModel = adapter.getItem(position);
        if (couponsModel == null) return;

        if (view.getId() == R.id.tv_get) {
            if (type == 4) {
                if (couponsModel.isSelect()) {
                    toast("请不要重复领取");
                    return;
                }
                couponReceive(position, couponsModel.getCoupon_id());
            } else if (type == 0) {
                GoodsListActivity.startActivity(getActivity(), GoodsListActivity.Coupons, couponsModel.getUser_coupon_id());
            }
        }


    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    @SuppressWarnings("unchecked")
    private void getData() {
        if (type == 4) {
            new HttpRequest<CouponsResponse>()
                    .with(getActivity())
                    .setApiCode(ApiCst.getListCoupon)
                    .addParam("p", page)
                    .addParam("psize", 10)
                    .addParam("token", App.getToken(mContext))
                    .setListener(new HttpRequest.OnNetworkListener<CouponsResponse>() {
                        @Override
                        public void onSuccess(CouponsResponse response) {
                            HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), page, 10);
                        }

                        @Override
                        public void onFail(String message, CouponsResponse response) {
                            page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                        }
                    }).start(new CouponsResponse());
            return;
        }
        new HttpRequest<CouponsResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.getCouponList)
                .addParam("status", type)
                .addParam("p", page)
                .addParam("psize", 10)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<CouponsResponse>() {
                    @Override
                    public void onSuccess(CouponsResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), page, 10);
                    }

                    @Override
                    public void onFail(String message, CouponsResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                    }
                }).start(new CouponsResponse());


    }

    private void couponReceive(final int position, int couponId) {
        new HttpRequest<DefaultResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.couponReceive)
                .addParam("token", App.getToken(mContext))
                .addParam("coupon_id", couponId)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        toast(response.message);
                        CouponsModel couponsModel = adapter.getItem(position);
                        if (couponsModel == null) return;
                        couponsModel.setSelect(true);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                    }
                }).start(new DefaultResponse());
    }

    public void refresh() {
//        if (recyclerView != null)
//            recyclerView.refresh();
    }
}
